/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.AbstractEntity;
import java.io.Serializable;
import org.ipti.br.tag.entity.Warning;
import org.ipti.br.tag.entity.Warning_;
import org.ipti.br.tag.entity.enums.WarningType;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author luancorumba
 */
@Stateless
public class WarningFacade extends AbstractFacade<Warning> implements Serializable {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    public WarningFacade() {
        super(Warning.class);
    }
    
    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    @Override
    public void create(Warning warning) {
        getEntityManager().persist(warning);
    }

    @Override
    public void edit(Warning warning) {
        getEntityManager().merge(warning);
    }
    
    public Warning findBy(WarningType type, AbstractEntity entity) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Warning> cq = cb.createQuery(Warning.class);
        Root<Warning> rt = cq.from(Warning.class);
        
        Predicate p = cb.equal(rt.get(Warning_.type), type);
//        Predicate p1 = cb.equal(rt.get(Warning_.target).get(AbstractEntity_.id), org.ipti.br.tag.entity.getId());
        Predicate p2 = cb.isFalse(rt.get(Warning_.resolved));
//        cq.where(p1,p2);
        
        try {
            return getEntityManager().createQuery(cq).getSingleResult();
        } catch (NoResultException nre) {
            return null;
        }
    }
    
    public List<Warning> getRoots() {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Warning> cq = cb.createQuery(Warning.class);
        Root<Warning> rt = cq.from(Warning.class);
        
        Predicate p1 = cb.isNull(rt.get(Warning_.father));
        Predicate p2 = cb.isFalse(rt.get(Warning_.resolved));
        cq.where(p1,p2);
        
        return getEntityManager().createQuery(cq).getResultList();
    }
    
    public List<Warning> getRoots(WarningType type) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Warning> cq = cb.createQuery(Warning.class);
        Root<Warning> rt = cq.from(Warning.class);
        
        Predicate p = cb.equal(rt.get(Warning_.type), type);
        Predicate p1 = cb.isNull(rt.get(Warning_.father));
        Predicate p2 = cb.isFalse(rt.get(Warning_.resolved));
        cq.where(p,p1,p2);
        
        return getEntityManager().createQuery(cq).getResultList();
    }
    
    public List<Warning> getLeafs(WarningType type) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Warning> cq = cb.createQuery(Warning.class);
        Root<Warning> rt = cq.from(Warning.class);
        
        Predicate p = cb.equal(rt.get(Warning_.type), type);
        Predicate p1 = cb.isNotNull(rt.get(Warning_.father));
        Predicate p2 = cb.isFalse(rt.get(Warning_.resolved));
        cq.where(p,p1,p2);
        
        return getEntityManager().createQuery(cq).getResultList();
    }
}
